/*
 * Exec.java
 *
 * Created on 2 Октябрь 2007 г., 12:07
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package xmppbot.parse;

import xmppbot.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smackx.muc.MultiUserChat;

/**
 *
 * @author lian
 */
public class Exec
{
    public options opts;
    public Date date;
    public String MsgType;         // Type of message:
                                   // "chat"       - simple chat
                                   // "groupchat"  - conference
                                   // "private"    - conference private
                                   // "gprivate"   - message in conference to bot
    public String AnsMsgType;      // Type of return message
    public String Msg;             // Message
    public String SplitMsg[];      // <space> splitted string
    public String postMsg;         //
    public int nextArg;            // next usable argument on the SplitMsg array
    public boolean executable;     // If message contain command var is true
    public boolean ansverable;     // 
    public boolean permittable;    //
    public boolean session;        //
    public String From;            // Sender's JID
    public String ShortFrom;       // Sender's short name
    public Packet Packet;          // Message packet
    public MultiUserChat mchat;    // Main muc chat to creat new chats
    public XMPPConnection connect; // Main connectin to creat new chats
    public db MySQLDB = Main.Connect.mysqldb;
    
    /** Creates a new instance of Exec */
    public Exec (options opts)
    {
        this.opts = opts;
    }
       
//    public void process () throws XMPPException, SQLException
//    {
//        String sessAnsver = null;
//        if (Main.sessions.containsKey(From)) {
//            session = true;
//            sessAnsver = ((xmppbot.funcs.func) Main.sessions.get(From)).process(this);
//        } else {
//            session = false;
//        }
//        //-----------------------------
//            user User = new user(ShortFrom);
//            SplitMsg = Msg.split(" ");
//            String max_spaces = (String) Main.Connect.settings.get("max_spaces_in_cmd");
//            ResultSet rs;
//            boolean flag = true;
//            int funcid = 0;
//            int i = 1;
//            String funcname = null;
//            while (flag && i <= Integer.parseInt(max_spaces) && i <= SplitMsg.length) {
//                flag = false;
//                String str = SplitMsg[0];
//                for (int j = 1; j<i; j++) {
//                    str = str + " " + SplitMsg[j];
//                }
//                rs = MySQLDB.getStatement().executeQuery("SELECT id,func FROM funcs WHERE id = (SELECT func FROM funcsvals WHERE val = '" + utils.parseSymbols(str) + "');");
//                funcname = null;
//                while (rs.next()) {
//                    funcname = rs.getString("func");
//                    funcid = rs.getInt("id");
//                }
//                rs.close();
//                if (funcname == null && SplitMsg.length > i) {
//                    flag = true;
//                    i++;
//                }
//            }
//            nextArg = i;
//            //System.out.println(funcname);
//            executable = false;
//            ansverable = false;
//            if (funcname != null) {
//                permittable = ((String) User.perms.get(funcname)).equals("1");
//            }
//            //System.out.println("test");
//            rs = MySQLDB.getStatement().executeQuery("SELECT exec,ans FROM sources WHERE cmd = (SELECT id FROM funcs WHERE func = '" + funcname + "') AND sources.from = (SELECT id FROM sourcevals WHERE val = '" + MsgType +"');");
//            if (rs.next()) {
//                executable = rs.getString("exec").equals("1");
//                ansverable = rs.getString("ans").equals("1");
//            }
//            rs.close();
//            if (SplitMsg.length != nextArg && executable) {
//                postMsg = SplitMsg[nextArg];
//                for (i = nextArg + 1; i < SplitMsg.length; i++) {
//                    postMsg = postMsg + " " + SplitMsg[i];
//                }
//            } else {
//                postMsg = null;
//            }
//            //--------------------------------
//        String ansver = null;
//        //System.out.println(permittable);
//        if (executable) {
//            if (permittable) {
//                try {ansver = ((xmppbot.funcs.func) Class.forName("xmppbot.funcs." + funcname).newInstance()).process(this);
//                } catch (InstantiationException ex) {ex.printStackTrace();} catch (ClassNotFoundException ex) {
//                    ex.printStackTrace();
//                } catch (IllegalAccessException ex) {ex.printStackTrace();}
//            } else {
//                try {ansver = ((xmppbot.funcs.func) Class.forName("xmppbot.funcs." + funcname).newInstance()).processnotallowed(this);
//                } catch (InstantiationException ex) {ex.printStackTrace();} catch (ClassNotFoundException ex) {
//                    ex.printStackTrace();
//                } catch (IllegalAccessException ex) {ex.printStackTrace();}
//                if (ansver == null) {
//                    rs = MySQLDB.getStatement().executeQuery("SELECT * FROM nonpermansvers;");
//                    ArrayList ansvers = new ArrayList();
//                    while (rs.next()) {
//                        ansvers.add(rs.getString("ansver"));
//                    }
//                    int id = Main.rand.nextInt(ansvers.size());
//                    ansver = (String) ansvers.get(id);
//                    ansverable = true;
//                    rs.close();
//                }
//            }
//        }
//            //------------------------------
//            if (ansverable) {
//                rs = MySQLDB.getStatement().executeQuery("SELECT val FROM sourcevals WHERE id = (SELECT sources.to FROM sources WHERE sources.from = (SELECT id FROM sourcevals WHERE val = '" + MsgType + "') AND cmd = (SELECT id FROM funcs WHERE func = '" + funcname + "'));");
//                rs.next();
//                AnsMsgType = rs.getString("val");
//            } else {
//                AnsMsgType = null;
//            }
//            rs.close();
//                //--------------------------------
//            //System.out.println("550");
//            if (sessAnsver != null) {
//                ansver = sessAnsver;
//            }
//            Chat anschat;
//            if (AnsMsgType != null && ansver != null) {
//                if (AnsMsgType.equals("chat")) {
//                    anschat = connect.getChatManager().createChat(From, new msglist());
//                    anschat.sendMessage(ansver);
//                } if (AnsMsgType.equals("groupchat")) {
//                    mchat.sendMessage(ansver);
//                } if (AnsMsgType.equals("private")) {
//                    anschat = mchat.createPrivateChat(From, new msglist());
//                    anschat.sendMessage(ansver);
//                } if (AnsMsgType.equals("gprivate")) {
//                    mchat.sendMessage(ShortFrom + ": " + ansver);
//                }
//            }
//            //---------------------- 
//        //System.out.println("551");
//            String AnsMsgType1;
//            if (AnsMsgType == null) {
//                AnsMsgType1 = "null";
//            } else {
//                AnsMsgType1 = "'" + AnsMsgType + "'";
//            }
//            int executableint;
//            int ansverableint;
//            if (executable) {
//                executableint = 1;
//            } else {
//                executableint = 0;
//            }
//            if (ansverable) {
//                ansverableint = 1;
//            } else {
//                ansverableint = 0;
//            }
//            String funcname1;
//            if (funcname == null) {
//                funcname1 = "null";
//            } else {
//                funcname1 = Integer.toString(funcid);
//            }
//            String ansver1;
//            if (ansver == null) {
//                ansver1 = "null";
//            } else {
//                ansver1 = "'" + ansver + "'";
//            }
//            //System.out.println("552");
//            String confname;
//            if (!MsgType.equals("chat")) {
//                rs = MySQLDB.getStatement().executeQuery("SELECT id FROM conferences WHERE conference = '" + utils.parseSymbols(mchat.getRoom()) + "';");
//                rs.next();
//                confname = rs.getString("id");
//            } else {
//                confname = "null";
//            }
//            rs.close();
//            int id = 0;
//            rs = MySQLDB.getStatement().executeQuery("SELECT id FROM allusers WHERE user = '" + utils.parseSymbols(From) + "'");
//            if (rs.next()) {
//                id = rs.getInt("id");
//            } else {
//                rs.close();
//                if (!MsgType.equals("chat")) {
//                    MySQLDB.getStatement().executeUpdate("INSERT INTO allusers VALUES (null,'" + utils.parseSymbols(From) + "','" + utils.parseSymbols(ShortFrom) + "',(SELECT id FROM conferences WHERE login = '" + utils.parseSymbols(mchat.getNickname()) + "' AND conference = '" + utils.parseSymbols(mchat.getRoom()) + "'),0);");
//                } else {
//                    MySQLDB.getStatement().executeUpdate("INSERT INTO allusers VALUES (null,'" + utils.parseSymbols(From) + "','" + utils.parseSymbols(ShortFrom) + "',null,0);");
//                }
//                rs = MySQLDB.getStatement().executeQuery("SELECT id FROM allusers WHERE user = '" + utils.parseSymbols(From) + "'");
//                rs.next();
//                id = rs.getInt("id");
//            }
//            MySQLDB.getStatement().executeUpdate("UPDATE allusers SET msgs = msgs+1 WHERE conference = (SELECT id FROM conferences WHERE login = '" + utils.parseSymbols(mchat.getNickname()) + "' AND conference = '" + utils.parseSymbols(mchat.getRoom()) + "') AND user = '" + utils.parseSymbols(From) + "';");
//            rs.close();
//            System.out.println(MsgType + "\t\t" + From + "\t\t" + Msg);
//            Message msg = new Message("lian@jabbus.org");
//            msg.setType(Message.Type.chat);
//            msg.setBody(MsgType + "\t\t" + From + "\t\t" + Msg);
//            Main.Connect.Connect.sendPacket(msg);
//            MySQLDB.getStatement().executeUpdate("INSERT INTO messagelog VALUES (null,'" + (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "','" + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds() + "'," + id + ",'" + utils.parseSymbols(ShortFrom) + "','" + utils.parseSymbols(Msg) + "',(SELECT id FROM sourcevals WHERE val = '" + MsgType + "'),(SELECT id FROM sourcevals WHERE val = " + AnsMsgType1 + ")," + executableint + "," + funcname1 + "," + ansverableint + "," + ansver1 + "," + utils.parseSymbols(confname) + ");");
//            //----------------
//    }
    public class msglist implements MessageListener {
        public void processMessage(Chat chat, Message message) {
        }   
    }
}
